package com.oauth.service.global;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oauth.logging.RapwavesLogger;
import com.oauth.logging.RapwavesLoggerFactory;
import com.oauth.service.application.registry.GlobalRegistryService;
import com.oauth.service.application.registry.beans.ApplicationInfoBean;
import com.oauth.service.global.beans.Birthday;
import com.oauth.service.global.beans.OAuthAccessToken;
import com.oauth.service.global.beans.OAuthRequestToken;
import com.rapwaves.feeds.Feeds;

/**
 * Use this service to do global functionality on the site.
 * 
 * @author asanthan
 * 
 */
@Service
public class GlobalServiceImpl implements GlobalService {

	/*
	 * Get birthdays from the next 20 days.
	 */
	private static final int BIRTHDAY_INTERVAL = 30;

	private RapwavesLogger logger = RapwavesLoggerFactory.getLogger(this.getClass());

	@Autowired
	private GlobalRegistryService<ApplicationInfoBean> globalRegistryService;

	/**
	 * @param globalRegistryService
	 *            the globalRegistryService to set
	 */
	public void setGlobalRegistryService(
			GlobalRegistryService<ApplicationInfoBean> globalRegistryService) {
		this.globalRegistryService = globalRegistryService;
	}

	public List<Long> updateStatus(Long pMemberId, Set<Long> pAppIds, String pStatusText) {
		Collection<ApplicationInfoBean> applicationList = globalRegistryService
				.getApplicationInfo(pAppIds);
		List<Long> failedApplicationList = new ArrayList<Long>(applicationList.size());

		for (ApplicationInfoBean appBean : applicationList) {
			try {
				appBean.getAppGlobalService().updateStatus(pMemberId, pStatusText);
			} catch (Exception e) {
				logger.error("Error in updating status for member=" + pMemberId + " with appId="
						+ appBean.getKey(), e);
				failedApplicationList.add(appBean.getKey());
			}
		}
		return failedApplicationList;
	}

	@Override
	public List<Birthday> getUpcomingBirthdays(Long pMemberId, Set<Long> pAppIds) {
		Collection<ApplicationInfoBean> applicationList = globalRegistryService
				.getApplicationInfo(pAppIds);
		List<Birthday> globalBirthdayList = new ArrayList<Birthday>();

		for (ApplicationInfoBean appBean : applicationList) {
			try {
				List<Birthday> birthdayList = appBean.getAppGlobalService().getUpcomingBirthdays(
						pMemberId, BIRTHDAY_INTERVAL);
				globalBirthdayList.addAll(birthdayList);
			} catch (Exception e) {
				logger.error("Error in getting birthdays for member=" + pMemberId + " with appId="
						+ appBean.getKey(), e);
			}
		}

		return globalBirthdayList;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.GlobalService#getOAuthRequestToken
	 * (java.lang.Long)
	 */
	@Override
	public OAuthRequestToken getOAuthRequestToken(Long applicationId) {
		ApplicationInfoBean applicationInfo = globalRegistryService
				.getApplicationInfo(applicationId);
		return applicationInfo.getAppGlobalService().getRequestToken();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.GlobalService#getOAuthAccessToken
	 * (java.lang.Long)
	 */
	@Override
	public OAuthAccessToken getOAuthAccessToken(Long applicationId, OAuthRequestToken requestToken) {
		ApplicationInfoBean applicationInfo = globalRegistryService
				.getApplicationInfo(applicationId);

		return applicationInfo.getAppGlobalService().getAccessToken(requestToken);
	}

	@Override
	public Feeds getFeeds(Long memberId, Long pAppId) throws Exception {
		ApplicationInfoBean applicationInfo = globalRegistryService.getApplicationInfo(pAppId);
		return applicationInfo.getAppGlobalService().getFeeds(memberId);
	}
}
